package com.pmsystem.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.pmsystem.pojo.Root;

@Component
public class RootInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println(request.getRequestURI() + "执行之前...");
		Root root = (Root) request.getSession().getAttribute("root");
		if (root == null) {
			System.out.println("需登录");
			response.setContentType("text/html;charset=utf-8");
			response.getWriter()
					.write("<script type='text/javascript'>alert('请登录!');window.location.href='login.jsp'</script>");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	//	System.out.println(request.getRequestURI() + "执行之后..." + modelAndView.getViewName());
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("执行结束");
	}
}
